/**
 * Copyright (C) 2011 Domenico Briganti <dometec@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package it.webrules.deploymanager.webapp.utils;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class CollectionUtils {

	public static String print(Map map) {

		StringBuilder out = new StringBuilder();

		Iterator iterator = map.keySet().iterator();

		while (iterator.hasNext()) {

			Object key = iterator.next();
			out.append(key + "=");

			Object value = map.get(key);
			if (value instanceof String[]) {
				for (String val : (String[]) value) {
					out.append("{" + val + ", ");
				}
				out.deleteCharAt(out.length() - 1);
				out.deleteCharAt(out.length() - 1);
				out.append("}");
			} else {
				out.append(value);
			}

			if (iterator.hasNext())
				out.append(", ");
		}

		return out.toString();

	}
	
	public static String print(Set set) {

		StringBuilder out = new StringBuilder();

		Iterator iterator = set.iterator();

		while (iterator.hasNext()) {

			Object key = iterator.next();
			out.append(key + "=");

			Object value = key;
			if (value instanceof String[]) {
				for (String val : (String[]) value) {
					out.append("{" + val + ", ");
				}
				out.deleteCharAt(out.length() - 1);
				out.deleteCharAt(out.length() - 1);
				out.append("}");
			} else {
				out.append(value);
			}

			if (iterator.hasNext())
				out.append(", ");
		}

		return out.toString();

	}
}
